//
//  WMH_User_Detail_ViewController.m
//  wmh.ios.v5
//
//  Created by AndLi on 14-8-29.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "WMH_User_Detail_ViewController.h"

#import "TDBadgedCell.h"

#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

#import "WMH_User_ChangePassword_ViewController.h"
#import "WMH_User_Edit_ViewController.h"



@interface WMH_User_Detail_ViewController ()
{
    NSString *istipmsg;
    
    NSString *name;
}
@property (weak, nonatomic) IBOutlet UITableView *mTableView;

@end

@implementation WMH_User_Detail_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"用户中心";
    
    if ([DBFrameService getConfigValue:WMH_ISTipMsg]==nil || [[DBFrameService getConfigValue:WMH_ISTipMsg] isEqualToString:@""]) {
        [DBFrameService setConfigValue:WMH_ISTipMsg value:@"否"];
    }
    
    istipmsg = [DBFrameService getConfigValue:WMH_ISTipMsg];
    
    //自定义右导航：采用系统样式
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}

#pragma 返回操作
-(void)backAction{
    [self back];
}

-(IBAction)clickExitButton:(id)sender{
    [self back];
    [DBFrameService setConfigValue:WMH_IsLogin value:@"0"];
}

-(void)back{
    if([[DBFrameService getConfigValue:WMH_IsLogin]isEqualToString:@"1"]){
        int count = self.navigationController.viewControllers.count;
        NSLog(@"count=%i",count);
        if (count==4) {
            UIViewController *viewControl =
            [self.navigationController.viewControllers objectAtIndex:count-3];
            [self.navigationController popToViewController:viewControl animated:YES];
        }else if (count==3){
            UIViewController *viewControl =
            [self.navigationController.viewControllers objectAtIndex:count-3];
            [self.navigationController popToViewController:viewControl animated:YES];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"Cell";
    TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    if ([indexPath indexAtPosition:0]==0) {
        
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = [DBFrameService getConfigValue:WMH_User_TrueName];
                cell.badgeString = @"退出登录";
                cell.badgeColor = COLOR(30, 144, 255);
                break;
            case 1:
                cell.textLabel.text = @"密码修改";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                break;
            case 2:
                cell.textLabel.text = @"资料修改";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                break;
                
            default:
                break;
        }
        return cell;
    }
    else if ([indexPath indexAtPosition:0]==1)
    {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"是否提示消息数量";
                cell.badgeString = istipmsg;
                cell.badgeColor = COLOR(30, 144, 255);
                break;
            case 1:
                cell.textLabel.text = @"清除缓存数据";
                break;
            default:
                break;
        }
        return cell;
    }
    return nil;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 3;
    }
    else if (section==1) {
        return 2;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([indexPath indexAtPosition:0]==0) {
        switch (indexPath.row) {
                //                退出登录
            case 0:
            {
                RIButtonItem *okItem = [RIButtonItem item];
                okItem.label = @"确定";
                okItem.action = ^
                {
                    [self back];
                    [DBFrameService setConfigValue:WMH_IsLogin value:@"0"];
                    
                };
                
                RIButtonItem *noItem = [RIButtonItem item];
                noItem.label = @"取消";
                
                //调用
                NSString *mess = [NSString stringWithFormat:@"确定退出吗？"];
                
                UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:mess cancelButtonItem:noItem otherButtonItems:okItem, nil];
                [alter show];
            }
                break;
                //            密码修改
            case 1:
            {
                WMH_User_ChangePassword_ViewController *view = [[WMH_User_ChangePassword_ViewController alloc]init];
                [self.navigationController pushViewController:view animated:YES];
            }
                break;
                //            资料修改
            case 2:
            {
                WMH_User_Edit_ViewController *view = [[WMH_User_Edit_ViewController alloc]init];
                [self.navigationController pushViewController:view animated:YES];
            }
                break;
                
            default:
                break;
        }
    }
    else if ([indexPath indexAtPosition:0]==1)
    {
        switch (indexPath.row) {
                //                是否提示消息数量
            case 0:
                if ([istipmsg isEqualToString:@"是"]) {
                    istipmsg = @"否";
                }else{
                    istipmsg = @"是";
                }
                [DBFrameService setConfigValue:WMH_ISTipMsg value:istipmsg];
                [_mTableView reloadData];
                break;
                //                清除缓存数据
            case 1:
            {
                [self clearAppCache];
            }
                break;
            default:
                break;
        }
    }
    
}


#pragma mark 清空缓存
-(void)clearAppCache{
    RIButtonItem *okItem = [RIButtonItem item];
    okItem.label = @"确定";
    okItem.action = ^
    {
        //清空缓存图片
        NSArray *documentspath=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString *docDir=[documentspath objectAtIndex:0];
        
        NSString *savePath=[docDir stringByAppendingPathComponent:wmh_cache_image];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        if(![fileManager fileExistsAtPath:[docDir stringByAppendingPathComponent:wmh_cache_image]])
        {
            [fileManager createDirectoryAtPath:savePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSString *extension = @"jpg";
        NSArray *contents = [fileManager contentsOfDirectoryAtPath:savePath error:NULL];
        NSEnumerator *e = [contents objectEnumerator];
        NSString *filename;
        while ((filename = [e nextObject])) {
            NSString * fileExtentsion =[filename pathExtension];
            if ([fileExtentsion isEqualToString:extension]) {
                
                [fileManager removeItemAtPath:[savePath stringByAppendingPathComponent:filename] error:NULL];
            }
        }
        
        //清空附件
        //            NSString *attachpath = [NSString stringWithFormat:@"%@/Documents/attaches/",NSHomeDirectory()];
        //            [fileManager removeItemAtPath:attachpath error:nil];
        
        
        //清空数据库缓存
        [DBFrameService deleteFrameCache];
        
        [self toastMessage:@"清空完成"];
        
    };
    
    RIButtonItem *noItem = [RIButtonItem item];
    noItem.label = @"取消";
    
    //调用
    NSString *mess = [NSString stringWithFormat:@"确定要清理缓存吗？"];
    
    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:mess cancelButtonItem:noItem otherButtonItems:okItem, nil];
    [alter show];
}

@end
